package com.example.work4.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.BR
import com.example.work4.R
import com.example.work4.adapter.viewHolder.ChatViewHolder
import com.example.work4.common.ImageUtil
import com.example.work4.http.body.po.ChatInfoPage

class ChatListAdapter(var data:List<ChatInfoPage> = ArrayList()): RecyclerView.Adapter<ChatViewHolder>() {
    var context:Context? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatViewHolder {
        context = parent.context
        return ChatViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent?.context),
                viewType,
                parent,
                false
            )
        )

    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.item_chat
    }

    override fun onBindViewHolder(holder: ChatViewHolder, position: Int) {
        val binding : ViewDataBinding = holder.dataBinding
        holder.info = data[position]
        if(position == 0) holder.notice.visibility = View.VISIBLE
        val imgUtil = ImageUtil(context!!)
        imgUtil.GlideLoadCenterCrop(holder.info.avatar,holder.avatar)
        binding.setVariable(BR.chatInfo, data[position])
    }

    override fun getItemCount(): Int {
       return data.size
    }

}